# -*- coding: utf-8 -*-

from odoo import http
from odoo_contrib import http as contrib_http
from odoo_contrib.parser import parse_with
from odoo_contrib.marshal import marshal_with

from ..schemas import parsers
from ..schemas import marshals

from ..utils.auth import Auth


@contrib_http.route('/wechat/auth')
class AuthController(contrib_http.Controller):
    @contrib_http.route('/login', auth='public', methods=['POST'])
    @parse_with(parsers.AuthParser)
    @marshal_with(marshals.WechatAuthSchema, envelope='data')
    def login(self, **kwargs):
        auth = Auth()
        params = self.parser.parse()
        user, token = auth.login(**params)
        return {
            'token': token,
            'user': user,
        }
